//
//  XLTabBarButton.m
//  MSCustomTabBarController
//
//  Created by 于鹏 on 2017/8/9.
//  Copyright © 2017年 MrSong. All rights reserved.
//

#import "XLTabBarButton.h"
#import "XLMainDefine.h"
#define kImgSize 35    //限定图片宽高
#define kImgTextDistance 5 //图片和文字的距离


@implementation XLTabBarButton

- (instancetype)initWithFrame:(CGRect)frame normalImage:(NSString *)normalImgName selectedImage:(NSString *)selectedImgName title:(NSString *)title normalTextColor:(UIColor *)normalColor selectedTextColor:(UIColor *)selectedColor titleFont:(UIFont *)font{
    self = [super initWithFrame:frame];
    if (self) {
        [self setImage:XLImage(normalImgName) forState:UIControlStateNormal];
        [self setImage:XLImage(selectedImgName) forState:UIControlStateDisabled];
//        [self setImage:XLImage(selectedImgName)forState:UIControlStateHighlighted];
        [self setImage:XLImage(normalImgName)forState:UIControlStateHighlighted];

        [self setTitle:title forState:UIControlStateNormal];
        [self setTitleColor:normalColor forState:UIControlStateNormal];
        [self setTitleColor:selectedColor forState:UIControlStateDisabled];
//        [self setTitleColor:selectedColor forState:UIControlStateHighlighted];
        [self setTitleColor:normalColor forState:UIControlStateHighlighted];

        self.titleLabel.font = font;
    }
    return self;
}

// button上的图片和文字 图片在左,文字在右,不居中显示,固定图片距离左侧
-(void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat buttonWidth = self.frame.size.width;
    CGFloat buttonHeight = self.frame.size.height;
    CGFloat imgWidth = kImgSize;
    CGFloat imgHeight = kImgSize;
    CGSize textSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGFloat textWitdh = textSize.width;
    CGFloat textHeight = textSize.height;
    
    
    CGRect imageFrame = self.imageView.frame;
    //根据UI调整
    imageFrame.origin.x = (buttonWidth/3 - imgWidth - kImgTextDistance)/2;
    imageFrame.origin.y = (buttonHeight - imgHeight)/2;
    imageFrame.size.width = imgWidth;
    imageFrame.size.height = imgHeight;
    self.imageView.frame =  imageFrame;
    
    CGRect textFrame = self.titleLabel.frame;
    textFrame.origin.x = CGRectGetMaxX(self.imageView.frame) + kImgTextDistance;
    textFrame.origin.y = (buttonHeight - textHeight)/2;
    textFrame.size.width = textWitdh;
    textFrame.size.height = textHeight;
    self.titleLabel.frame = textFrame;
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    
    
}


@end
